package com.samphanie.dries.validation;

import com.samphanie.dries.annotation.ValidPassword;
import lombok.RequiredArgsConstructor;
import org.passay.*;
import org.passay.spring.SpringMessageResolver;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * @Description: 密码验证策略
 * @Author ZSY
 * @createTime 2021/2/25 15:16
 */
@RequiredArgsConstructor
public class PasswordConstraintValidator implements ConstraintValidator<ValidPassword, String> {

    private final SpringMessageResolver springMessageResolver;

    @Override
    public void initialize(final ValidPassword constraintAnnotation) {

    }

    @Override
    public boolean isValid(final String password, final ConstraintValidatorContext context) {

        PasswordValidator passwordValidator = new PasswordValidator(springMessageResolver, Arrays.asList(
                new LengthRule(8, 30),
                new CharacterRule(EnglishCharacterData.UpperCase, 1),
                new CharacterRule(EnglishCharacterData.LowerCase, 1),
                new CharacterRule(EnglishCharacterData.Special, 1),
                new IllegalSequenceRule(EnglishSequenceData.Alphabetical, 5, false),
                new IllegalSequenceRule(EnglishSequenceData.Numerical, 5, false),
                new IllegalSequenceRule(EnglishSequenceData.USQwerty, 5, false),
                new WhitespaceRule()
        ));

        RuleResult result = passwordValidator.validate(new PasswordData(password));

        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(String.join(
                ",", passwordValidator.getMessages(result)
        )).addConstraintViolation();

        return result.isValid();
    }
}
